#include "settingitem.h"
#include <QColorDialog>
#include <QFontDialog>
#include <QInputDialog>

SettingItem::SettingItem(QString aKey)
{
    m_key = aKey;
    m_type = ESettingString;
    m_intValue = 0;
    m_boolValue = false;
}

SettingItem::SettingItem(QString aKey, QString aValue)
{
    m_key = aKey;
    m_type = ESettingString;
    m_stringValue = aValue;
}

SettingItem::SettingItem(QString aKey, int aValue)
{
    m_key = aKey;
    m_type = ESettingInt;
    m_intValue = aValue;
}

SettingItem::SettingItem(QString aKey, QColor aValue)
{
    m_key = aKey;
    m_type = ESettingColor;
    m_colorValue = aValue;
}

SettingItem::SettingItem(QString aKey, QFont aValue)
{
    m_key = aKey;
    m_type = ESettingFont;
    m_fontValue = aValue;
}


SettingItem::SettingItem(QString aKey, bool aValue)
{
    m_key = aKey;
    m_type = ESettingBool;
    m_boolValue = aValue;
}


QString SettingItem::getHtmlStyle()
{

    if(m_type == ESettingColor)
    {
        QString content;
        if(m_key.contains("background"))
            content += "color:black;background-color:" +
                m_colorValue.name() + ";";
        else if(m_key.contains("foreground"))
            content += "color:" +
                m_colorValue.name() + ";";
        return content;
    }
    else if(m_type == ESettingFont)
    {
        QString fontStyle;
        if(m_fontValue.bold())
            fontStyle += "font-weight:bold;";
        else
            fontStyle += "font-weight:normal;";
        if(m_fontValue.italic())
            fontStyle += "font-style:italic;";
        else
            fontStyle += "font-style:normal;";
        fontStyle += "font-family:" + m_fontValue.family() + ";";
        if(m_fontValue.underline())
            fontStyle += "text-decoration:underline;";
        else
            fontStyle += "text-decoration:none;";
        QString scale = "pt";
        int fontSize = m_fontValue.pointSize();
        if(fontSize < 0)
        {
            fontSize = m_fontValue.pixelSize();
            scale = "px";
        }

        fontStyle += "font-size:" + QString::number(fontSize) + scale +  ";";
        //fontStyle += "color:#000000;";
        return fontStyle;
    }
    return "";
}


QString SettingItem::getEditorContent()
{
    QString content;
    if(m_type == ESettingColor || m_type == ESettingFont)
        content += "<span style='" + getHtmlStyle() + "'>Example</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    else if(m_type == ESettingString)
        content += "<span style='font-weight:bold;color:black;'>"
                + m_stringValue + "</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    else if(m_type == ESettingInt)
        content += "<span style='font-weight:bold;color:black;'>"
                + QString::number(m_intValue) + "</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    else if(m_type == ESettingBool)
    {
        if(m_boolValue)
            content += "<span style='font-weight:bold;color:black;'>true</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        else
            content += "<span style='font-weight:bold;color:black;'>false</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    }


    content += "<a href='set:" + m_key + "'>Change</a>";
    return content;
}

SettingItem::SettingItem()
{
    m_key = "setting.default";
    m_type = ESettingString;
    m_intValue = 0;
    m_boolValue = false;
}

QString SettingItem::getKey() const
{
    return m_key;
}

QColor SettingItem::getColorValue() const
{
    return m_colorValue;
}

int SettingItem::getIntValue() const
{
    return m_intValue;
}

QString SettingItem::getStringValue() const
{
    return m_stringValue;
}

bool SettingItem::getBoolValue() const
{
    return m_boolValue;
}

QFont SettingItem::getFontValue() const
{
    return m_fontValue;
}

SettingsTypeEnum SettingItem::getType() const
{
    return m_type;
}

void SettingItem::setColorValue(QColor aColor)
{
    m_colorValue = aColor;
}

void SettingItem::setIntValue(int aValue)
{
    m_intValue = aValue;
}

void SettingItem::setStringValue(QString aValue)
{
    m_stringValue = aValue;
}

void SettingItem::setBoolValue(bool aValue)
{
    m_boolValue = aValue;
}

void SettingItem::setType(SettingsTypeEnum aType)
{
    m_type = aType;
}

void SettingItem::setFontValue(QFont aFont)
{
    m_fontValue = aFont;
}




SettingItem &SettingItem::operator =(SettingItem &aItem)
{
    if(this == &aItem)
        return *this;
    m_type = aItem.getType();
    m_key = aItem.getKey();
    m_colorValue = aItem.getColorValue();
    m_intValue = aItem.getIntValue();
    m_boolValue = aItem.getBoolValue();
    m_stringValue = aItem.getStringValue();
    m_fontValue = aItem.getFontValue();
    return *this;
}



SettingItem &SettingItem::operator =(const SettingItem &aItem)
{
    if(this == &aItem)
        return *this;
    m_type = aItem.getType();
    m_key = aItem.getKey();
    m_colorValue = aItem.getColorValue();
    m_intValue = aItem.getIntValue();
    m_boolValue = aItem.getBoolValue();
    m_stringValue = aItem.getStringValue();
    m_fontValue = aItem.getFontValue();
    return *this;
}

void SettingItem::write(QSettings &aSettings)
{
    switch(m_type)
    {
        case ESettingString:
            aSettings.setValue(m_key, m_stringValue);
        case ESettingInt:
            aSettings.setValue(m_key, m_intValue);
        break;
        case ESettingFont:
            aSettings.setValue(m_key, m_fontValue);
        break;
        case ESettingColor:
            aSettings.setValue(m_key, m_colorValue);
        break;
        case ESettingBool:
            aSettings.setValue(m_key, m_boolValue);
        break;
        default:
        {
            aSettings.setValue(m_key, m_stringValue);
        }
    }
}

void SettingItem::read(QSettings &aSettings)
{
    if(!aSettings.contains(m_key))
        return;
    switch(m_type)
    {
        case ESettingString:
            m_stringValue = aSettings.value(m_key).toString();
        case ESettingInt:
        {
            bool ok = false;
            int temp = aSettings.value(m_key).toInt(&ok);
            if(ok)
                m_intValue = temp;
        }
        break;
        case ESettingFont:
        {
            QVariant temp = aSettings.value(m_key);
            m_fontValue = temp.value<QFont>();
        }
        break;
        case ESettingColor:
        {
            QVariant temp = aSettings.value(m_key);
            m_colorValue = temp.value<QColor>();
        }
        break;
        case ESettingBool:
        {
            m_boolValue = aSettings.value(m_key).toBool();
        }
        break;
        default:
        {
            aSettings.setValue(m_key, m_stringValue);
        }
    }
}



void SettingItem::processLink()
{
    if(m_type == ESettingColor)
    {
        QColor newColor = QColorDialog::getColor(m_colorValue);
        m_colorValue = newColor;
    }
    else if(m_type == ESettingFont)
    {
        bool ok = false;
        QFont newFont = QFontDialog::getFont(&ok, m_fontValue);
        if(ok)
            m_fontValue = newFont;
    }
    else if(m_type == ESettingString)
    {
        bool ok = false;
        QString result = QInputDialog::getText(NULL, "", "",
                                               QLineEdit::Normal, result, &ok);
        if(ok)
            m_stringValue = result;
    }
    else if(m_type == ESettingInt)
    {
        bool ok = false;
        int result = QInputDialog::getInt(NULL, "", "",
                                                result, m_intValue, 1024, 1, &ok);
        if(ok)
            m_intValue = result;
    }
    else if(m_type == ESettingBool)
    {
        m_boolValue = !m_boolValue;
    }
}
